package com.shihang.crm.service;

import com.shihang.crm.bean.Customer;
import com.shihang.crm.dao.CustomDao;
import com.shihang.crm.utils.PageBean;
import org.hibernate.criterion.DetachedCriteria;

import java.util.List;

public class CustomerServiceImpl implements CustomerService{
    private CustomDao cd;

    @Override
    public PageBean getPageBean(DetachedCriteria dc, int currentPage, int pageSize) {
        //1.调用dao查询总记录
        int totalCount = cd.getTotalCount(dc);
        //2.创建pageBean对象
        PageBean pageBean = new PageBean(currentPage,totalCount,pageSize);
        //3调用dao查询分页列表
        List<Customer> list = cd.getPageList(dc,pageBean.getStart(),pageBean.getPageSize());
        //4.列表数据放入pageBean中
        pageBean.setList(list);
        return pageBean;
    }

    @Override
    public void save(Customer customer) {
        //1.维护customer与数据字典的关系，由于struts的参数封装，会将参数封装到数据字典的id属性
        //我们无需手动维护关系

        //2.调用Dao保存客户
        cd.save(customer);

    }

    @Override
    public void saveOrUpDate(Customer customer) {
        cd.saveOrUpdate(customer);
    }

    @Override
    public Customer getById(Customer customer) {
        return cd.getById(customer.getCust_id());
    }

    public void setCd(CustomDao cd) {
        this.cd = cd;
    }
}
